<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let arr = [];
      for (let index = 0; index < 5; index++) {
        let age = prompt(`输入第${index + 1}个年龄`);
        if (age > 0 && age < 100) {
          arr.push(age);
        } else {
          alert('你输入的年龄无效');
        }
      }

      for (let index = 0; index < arr.length; index++) {
        const element = arr[index];
        if (element > 18) {
          console.log(element);
        }
      }

      function getSum() {
        let sum = 0;
        for (let index = 0; index < arr.length; index++) {
          const element = arr[index];
          sum += element;
        }
        return sum;
      }
      console.log(getSum());

      function getAvg() {
        let sum = 0;
        let average = 0;
        for (let index = 0; index < arr.length; index++) {
          const element = arr[index];
          sum += element;
        }
        return (average = sum / arr.length);
      }
      console.log(getAvg());

      function getMax() {
        let max = arr[0];
        for (let index = 0; index < arr.length; index++) {
          const element = arr[index];
          if (max < arr[index]) {
            max = arr[index];
          }
        }
        return max;
      }
      console.log(getMax());

      function getMin() {
        let min = arr[0];
        for (let index = 0; index < arr.length; index++) {
          const element = arr[index];
          if (min > arr[index]) {
            min = arr[index];
          }
        }
        return min;
      }
      console.log(getMin());
    </script>
  </body>
</html>
